﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaiTap1
{
    public class BieuThucDon : BieuThuc
    {
        #region - Attribute
        private double _heSo;
        private int _soMu;
        #endregion

        #region - Property
        public double HeSo
        {
            get { return _heSo; }
            set { _heSo = value; }
        }
        public int SoMu
        {
            get { return _soMu; }
            set { _soMu = value; }
        }
        #endregion

        #region - Method
        public BieuThucDon(double HeSo)
        {
            _heSo = HeSo;
            _soMu = 0;
        }
        public BieuThucDon(int SoMu, double HeSo)
        {
            _heSo = HeSo;
            _soMu = SoMu;
        }
        public override double TinhGiaTri(double x)
        {
            return HeSo * Math.Pow(x, SoMu);
        }
        public override string TinhDaoHam()
        {
            BieuThuc ketQua;
            if (_soMu == 0)
                ketQua = new BieuThucDon(_soMu, 0);
            else
                ketQua = new BieuThucDon(_soMu - 1, _heSo * _soMu);
            return ketQua.ToString();
        }
        public override string ToString()
        {
            if (SoMu == 0)
                if (HeSo == 0)
                    return string.Empty;
                else
                    return HeSo.ToString();
            string ketQua = string.Empty;
            if (HeSo != 1)
                ketQua += HeSo.ToString();
            ketQua += "x";
            if (SoMu != 1)
                ketQua += "^" + SoMu;
            return ketQua;
        }
        public static BieuThucDon Convert(string input)
        {
            double heSo = 1;
            int soMu = 1;
            if (input == "")
                return null;
            else
            {
                if(input.Contains("x"))
                {
                    if(input.Contains("^"))
                    {
                        int index = input.IndexOf('x');
                        if (index == 0) //x^12
                        {
                            string tmp = input.Substring(2, input.Count()-2);
                            soMu = int.Parse(tmp);
                            heSo = 1;
                        }
                        else  //2x^5
                        {
                            string tmp1 = input.Substring(0, index);
                            string tmp2 = input.Substring(index + 2, input.Count() - index - 2);
                            heSo = double.Parse(tmp1);
                            soMu = int.Parse(tmp2);
                        }
                    }
                    else  //12x
                    {
                        int index = input.IndexOf('x');
                        string tmp = input.Substring(0, index);
                        heSo = double.Parse(tmp);
                        soMu = 1;
                    }
                }
                else //12                
                {
                    heSo = int.Parse(input);
                    soMu = 0;
                }
            }
            BieuThucDon ketQua = new BieuThucDon(soMu, heSo);
            return ketQua;
        }
        #endregion
    }
}
